package in.muton.edushare.components.messaging.util;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.roles.DeptRoleCheck;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Messages;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class Recipients {
	
	private static Log log = LogFactory.getLog(Recipients.class);
	public static List<Users> getRecipients(Object reciever, int privacy) {
		if (reciever instanceof Universities) {
			return getUnivList((Universities) reciever, privacy);
		} else if (reciever instanceof Departments) {
			return getDeptList((Departments) reciever, privacy);
		} else if (reciever instanceof CourseInstances) {
			return getCourseList((CourseInstances) reciever, privacy);
		} else if (reciever instanceof Users) {
			return getUserList((Users) reciever, privacy);
		} else
			return new ArrayList<Users>();
	}

	private static List<Users> getUnivList(Universities univ, int privacy) {
		List<UserRoles> roles = univ.getUserRoleList();
		List<Users> userList = new ArrayList<Users>();
		for (UserRoles r : roles) {
			if (r.getRoleType() == UserRoles.ADMIN_ROLE) {
				userList.add(r.getUser());
			} else if (r.getRoleType() == UserRoles.TEACHER_ROLE
					&& (privacy != Messages.PRIVACY_TYPE_PRIVATE)) {
				userList.add(r.getUser());
			} else if (r.getRoleType() == UserRoles.STUDENT_ROLE
					&& (privacy == Messages.PRIVACY_TYPE_PUBLIC || privacy == Messages.PRIVACY_TYPE_NONE)) {
				userList.add(r.getUser());
			}
		}
		if (!userList.contains(EduShareSession.get().getLoggedInUser())) {
			userList.add(EduShareSession.get().getLoggedInUser());
		}
		return userList;
	}

	private static List<Users> getDeptList(Departments dept, int privacy) {
		List<Users> userList = new ArrayList<Users>();
		List<UserDeptLink> udl = dept.getUserLinks();
		for (UserDeptLink u : udl) {
			DeptRoleCheck drc = new DeptRoleCheck(dept, u.getUserLink());
			if (drc.isPrimaryAdmin()) {
				userList.add(u.getUserLink());
			} else if ((privacy != Messages.PRIVACY_TYPE_PRIVATE)
					&& (drc.isSecondaryAdmin() || drc.isTeacher())) {
				userList.add(u.getUserLink());
			} else if (drc.isStudent()
					&& (privacy == Messages.PRIVACY_TYPE_PUBLIC || privacy == Messages.PRIVACY_TYPE_NONE)) {
				userList.add(u.getUserLink());
			}
		}
		if (!userList.contains(EduShareSession.get().getLoggedInUser())) {
			userList.add(EduShareSession.get().getLoggedInUser());
		}
		return userList;
	}

	private static List<Users> getCourseList(CourseInstances course, int privacy) {
		List<Users> userList = new ArrayList<Users>();
		List<Enrolments> enrol = course.getEnrolmentList();
		
		log.debug("Privacy level = "+ privacy);
		for (Enrolments e : enrol) {
			if (e.getEnrolmentType() == Enrolments.TEACHER)
				userList.add(e.getUser());
			else if ((e.getEnrolmentType() == Enrolments.TEACHING_ASSISTANT)
					&& (privacy != Messages.PRIVACY_TYPE_PRIVATE))
				userList.add(e.getUser());
			else if ((e.getEnrolmentType() == Enrolments.STUDENT||e.getEnrolmentType() == Enrolments.OBSERVER)
					&& (privacy == Messages.PRIVACY_TYPE_PUBLIC || privacy == Messages.PRIVACY_TYPE_NONE))
				userList.add(e.getUser());
		}
		
		for(Users u:userList){
			log.debug("User = "+ u.getCompleteUserName());
		}
		
		if (!userList.contains(EduShareSession.get().getLoggedInUser())) {
			userList.add(EduShareSession.get().getLoggedInUser());
		}
		return userList;
	}

	private static List<Users> getUserList(Users user, int privacy) {
		List<Users> userList = new ArrayList<Users>();
		userList.add(user);
		if (!userList.contains(EduShareSession.get().getLoggedInUser())) {
			userList.add(EduShareSession.get().getLoggedInUser());
		}
		return userList;
	}

}
